var pool = require("./config/db.js"),
    markdown = require('markdown').markdown;

/**
 * 设施
 * @param facilities
 * @constructor
 */
function HotelFacilities(facilities){
    if(facilities){
        this.hotel_id=facilities.hotel_id;
        this.airport=facilities.airport;
        this.babysit=facilities.babysit;
        this.bar=facilities.bar;
        this.nearbeach=facilities.nearbeach;
        this.parking=facilities.parking;
        this.alarm=facilities.alarm;
        this.conference=facilities.conference;
        this.elevator=facilities.elevator;
        this.entertainment=facilities.entertainment;
        this.gym=facilities.gym;
        this.safe=facilities.safe;
        this.internet=facilities.internet;
        this.iron=facilities.iron;
        this.laundry=facilities.laundry;
        this.mailbox=facilities.mailbox;
        this.moneyexchange=facilities.moneyexchange;
        this.satelite=facilities.satelite;
        this.smoking=facilities.smoking;
        this.spa=facilities.spa;
        this.pool=facilities.pool;
        this.heating=facilities.heating;
        this.ac=facilities.ac;
        this.pets=facilities.pets;
    }
};

module.exports = HotelFacilities;

/**
 * 增
 * @param callback
 */
HotelFacilities.prototype.save = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        airport:this.airport,
        babysit:this.babysit,
        bar:this.bar,
        nearbeach:this.nearbeach,
        parking:this.parking,
        alarm:this.alarm,
        conference:this.conference,
        elevator:this.elevator,
        entertainment:this.entertainment,
        gym:this.gym,
        safe:this.safe,
        internet:this.internet,
        iron:this.iron,
        laundry:this.laundry,
        mailbox:this.mailbox,
        moneyexchange:this.moneyexchange,
        satelite:this.satelite,
        smoking:this.smoking,
        spa:this.spa,
        pool:this.pool,
        heating:this.heating,
        ac:this.ac,
        pets:this.pets
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `hotel_facilities` SET ?',
            _obj,
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
HotelFacilities.prototype.delete = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `hotel_facilities` WHERE hotel_id=?',
            [_id],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
HotelFacilities.prototype.update = function(callback) {
    var _obj = {
        hotel_id:this.hotel_id,
        airport:this.airport,
        babysit:this.babysit,
        bar:this.bar,
        nearbeach:this.nearbeach,
        parking:this.parking,
        alarm:this.alarm,
        conference:this.conference,
        elevator:this.elevator,
        entertainment:this.entertainment,
        gym:this.gym,
        safe:this.safe,
        internet:this.internet,
        iron:this.iron,
        laundry:this.laundry,
        mailbox:this.mailbox,
        moneyexchange:this.moneyexchange,
        satelite:this.satelite,
        smoking:this.smoking,
        spa:this.spa,
        pool:this.pool,
        heating:this.heating,
        ac:this.ac,
        pets:this.pets
    };
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `hotel_facilities` SET ? WHERE hotel_id =?',
            [
                _obj,
                _obj.hotel_id
            ],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param callback
 */
HotelFacilities.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `hotel_facilities` WHERE hotel_id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
HotelFacilities.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM `hotel_facilities`',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from `hotel_facilities` ORDER BY `hotel_id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};
